package com.code9xs.controller;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.code9xs.pojo.Association;
import com.code9xs.pojo.User;
import com.code9xs.service.ManageService;
import com.code9xs.util.PageUtil;
import com.code9xs.util.Result;
import oracle.jrockit.jfr.events.EventControl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.Map;

/**
 * @author Suhd
 * @date 2021/5/24
 * @time 11:56
 * @wechat i9xswanan
 * @github https://github.com/Code9xs
 */
@RestController
@RequestMapping("/manage")
public class ManageController extends BaseController{
    @Autowired
    private ManageService manageService;

    @GetMapping("/user")
    public Result getAllUserInfo(@RequestParam("page")String page,@RequestParam("pageSize") String pageSize){
        PageUtil data = manageService.getAllUserInfo(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @PostMapping("/modify/user")
    public Result modifyUserInfo(@RequestBody JSONObject param){
        User user = new User();
        BCryptPasswordEncoder bc = new BCryptPasswordEncoder();
        user.setId(param.getInteger("id"));
        user.setUsername(param.getString("username"));
        user.setGender(param.getString("gender"));
        user.setPhone(param.getString("phone"));
        user.setEmail(param.getString("email"));
        user.setStudentNo(param.getString("studentNo"));
        Boolean resetAvatar = param.getBoolean("resetAvatar");
        Boolean resetPassword = param.getBoolean("resetPassword");
        if (resetPassword){
            final String encode = bc.encode("123456");
            user.setPassword(encode);
        }
        if (resetAvatar){
            user.setAvatar("http://little-corner.top/avatar1619491060000.jpg");
        }
        JSONArray rolesArray = param.getJSONArray("roles");
        Boolean flag = manageService.modifyUserInfo(user, rolesArray);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/delete/user")
    public Result deleteUser(@RequestBody JSONObject param){
        final boolean flag = manageService.deleteUser(param.getInteger("id"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/association")
    public Result getAllAssociation(@RequestParam("page")String page,@RequestParam("pageSize") String pageSize){
        PageUtil data = manageService.getAllAssociation(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @PostMapping("/modify/association")
    public Result modifyAssociation(@RequestBody JSONObject param){
        Association association = new Association();
        association.setId(param.getInteger("id"));
        association.setName(param.getString("name"));
        association.setType(param.getString("type"));
        association.setStatus(param.getString("status"));
        Boolean resetAvatar = param.getBoolean("resetAvatar");
        if (resetAvatar){
            association.setAssociationAvatar("http://little-corner.top/association_avatar1619659979000.jpg");
        }
        boolean flag = manageService.modifyAssociation(association);
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @PostMapping("/delete/association")
    public Result deleteAssociation(@RequestBody JSONObject param){
        final boolean flag = manageService.deleteAssociation(param.getInteger("id"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }

    @GetMapping("/article")
    public Result getAllArticle(@RequestParam("page")String page,@RequestParam("pageSize") String pageSize){
        PageUtil data = manageService.getAllArticle(Integer.valueOf(page), Integer.valueOf(pageSize));
        return Result.succ(HttpServletResponse.SC_OK,"成功",data);
    }

    @PostMapping("/delete/article")
    public Result deleteArticle(@RequestBody JSONObject param){
        final boolean flag = manageService.deleteArticle(param.getInteger("id"));
        if (flag){
            return Result.succ(HttpServletResponse.SC_OK,"成功",null);
        }
        return Result.fail(HttpServletResponse.SC_FORBIDDEN,"失败",null);
    }
}
